<?php
declare (strict_types = 1);

namespace app\controller\api\v1\user\mine;

use app\model\communal\UserWechatOpenModel;
use app\service\logic\common\course\CourseService;
use app\service\logic\common\user\CourseCollectService;
use app\service\logic\common\user\ExpertEvaluateService;
use app\service\logic\common\user\ExpertFollowService;
use app\service\logic\common\user\ExpertService;
use app\service\logic\common\user\StatisticsService;
use app\service\logic\common\user\UserService;
use app\service\logic\common\user\UserWechatOpenService;
use app\validate\common\course\CourseValidate;
use app\validate\common\user\UserValidate;
use app\validate\common\user\WechatOpenValidate;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Request;
use think\App;
use think\response\Json;
use app\controller\api\ApiController;

class MineController extends ApiController
{
    protected $service;

    public function __construct(App $app, UserService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 我的信息
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function info()
    {
        $user = $this->request->middleware('user');
        $result = $this->service->read(['id' => $user['id']], []);
        return amis($result);
    }

    /**
     * 完善信息
     *
     * @param Request $request
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function complete(Request $request)
    {
        $params = $request->only($this->service->getUpdateColumn());
        $user = $this->request->middleware('user');
        $params['id'] = $user['id'];
        $this->validate($params, UserValidate::class . '.complete');
        $params['gender'] = $params['gender'] ?? 1;

        $result = $this->service->item($params);
        return amis($result);
    }

    /**
     * @return Json
     */
    public function number()
    {
        $user = $this->request->middleware('user');
        $result = $this->service->getNumberByUserId($user['id']);
        return amis($result);
    }

    /**
     * 我的元心币
     *
     * @return Json
     */
    public function coin()
    {
        $user = $this->request->middleware('user');
        $result = invoke(StatisticsService::class)->getCoinByUserId($user['id']);
        return amis($result);
    }

    /**
     * 我关注的专家
     *
     * @return Json
     */
    public function experts()
    {
        $params = $this->request->param();
        $user = $this->request->middleware('user');
        $expertIds = invoke(ExpertFollowService::class)->getExpertIdsByUserId($user['id']);
        $expertIds = empty($expertIds) ? [-1] : $expertIds;
        $search_fields = ['ids'];
        $search = ['ids' => $expertIds];

        $fun = function ($model) {
            // 评价数量
            $model['eval_number'] = invoke(ExpertEvaluateService::class)->getNumberByExpertId($model['id']);
            return $model;
        };
        $result = invoke(ExpertService::class)->getList($params, $search_fields, $search, [], $fun);
        return amis($result);
    }

    /**
     * 我关注的课程
     *
     * @return Json
     */
    public function courses()
    {
        $params = $this->request->param();
        $user = $this->request->middleware('user');
        $courseIds = invoke(CourseCollectService::class)->getCourseIdsByUserId($user['id']);
        $courseIds = empty($courseIds) ? [-1] : $courseIds;
        $search_fields = ['ids'];
        $search = ['ids' => $courseIds];
        $this->validate($params, CourseValidate::class . '.index');

        $fun = function ($model) {
            // 类型 1 直播课程 2 点播课程
            $typeMap = [1 => '直播课程', 2 => '点播课程'];
            $model['type_map'] = $typeMap[$model['type']];
            unset($model['belongCategory'], $model['byCreator']);
            return $model;
        };
        $result = invoke(CourseService::class)->getList($params, $search_fields, $search, ['belongCategory', 'byCreator'], $fun);
        return amis($result);
    }

    /**
     * 是否绑定微信
     *
     * @return Json
     */
    public function isBind()
    {
        $user = $this->request->middleware('user');
        $result = invoke(UserWechatOpenService::class)->isBindByUserId($user['id']);
        return amis($result);
    }

    /**
     * 绑定微信
     *
     * @return Json
     */
    public function bind()
    {
        $params = $this->request->param();
        $this->validate($params, WechatOpenValidate::class . '.code');
        $user = $this->request->middleware('user');
        invoke(UserWechatOpenService::class)->bindByUserIdCode($user['id'], $params);
        return amis();
    }
}
